/*
 * JVM_CP_Utf8.c
 *
 *  Created on: 12.01.2011
 *      Author: Tavin
 */


#include "JVM_CP_Utf8.h"
#include "JVM_StatusCodes.h"


/****************************************************************************************/
/*																						*/
/*	Function : JVM_CP_Utf8_getStringSize					Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.12			Status : Working   			*/
/*																						*/
/****************************************************************************************/
uint16_t JVM_CP_Utf8_getStringSize(JVM_CP_UTF8_t * myUtf8)
{
	uint16_t counter;
	uint16_t size;

	size=0;


	for (counter=0;counter<JVM_CP_Utf8_getSize(myUtf8);counter++)
		{
		if((myUtf8->data[counter]&0b11000000)== 0b11000000)
			{
			counter++;
			}
			else if((myUtf8->data[counter]&0b11100000)== 0b11100000)
				{
				counter+=2;
				}

		size+=1;
		}



return(size);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_CP_Utf8_copyString						Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.12			Status : Working   			*/
/*																						*/
/****************************************************************************************/
uint16_t JVM_CP_Utf8_copyString(JVM_CP_UTF8_t * myUtf8,uint16_t * charArray)
{
	uint16_t counter;
	uint16_t index;

	index=0;


	for (counter=0;counter<JVM_CP_Utf8_getSize(myUtf8);counter++)
		{
		if(myUtf8->data[counter]<0x80)
			{
			*(charArray+index++)=myUtf8->data[counter];
			}
		else if((myUtf8->data[counter]&0b11000000)== 0b11000000)
			{
			*(charArray+index)=((myUtf8->data[counter++]&0x11111)<<6);
			*(charArray+index++)|=(myUtf8->data[counter]&0x111111);

			}
			else if((myUtf8->data[counter]&0b11100000)== 0b11100000)
				{
				*(charArray+index)=((myUtf8->data[counter++]&0x1111)<<12);
				*(charArray+index)|=((myUtf8->data[counter++]&0x111111)<<6);
				*(charArray+index++)|=((myUtf8->data[counter]&0x111111));
				}
			else
				{
				return(STCD_WRONG_UTF8_FORMAT);
				}

		}



	return(STCD_OK);
}
